package com.sinosoft.cpyy.controller.admin;

import com.sinosoft.cpyy.dao.master.AdminMapper;
import com.sinosoft.cpyy.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 创建时间：2018/三月/27
 *
 * @author 张政[zhang_zheng@sinosoft.com.cn]
 * 类名：AdminController
 * 描述：授权登录
 */
@Controller
public class AdminController {

    private final AdminMapper adminMapper;

    @Autowired
    public AdminController(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    /**
     * 映射主页模板
     *
     * @return
     */
    @RequestMapping(value = "/login.html", method = RequestMethod.GET)
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    /**
     * 登陆接受JSON数据
     *
     * @param session
     * @param users
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginJson(HttpSession session, Users users) {
        Users user = adminMapper.findByUser(users);
        if (user != null) {
            session.setAttribute("myUser", user);
            Integer permission = user.getPermission();
            if (permission == 1) {
                // 跳转管理员
                return "2";
            } else {
                // 跳转用户考试选题界面
                return "3";
            }
        }

        return "1";
    }

    @RequestMapping(value = "/admin/index.html", method = RequestMethod.GET)
    public ModelAndView admin() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("_admin/index");
        return modelAndView;
    }

}
